package com.zhihaoliang.baseframe;

import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * 创建日期：2020-02-24
 * 描述:这是一个为整个应用使用的执行池
 * 创建这个任务池的作用是避免出现不同类型的任务进行等待
 * 作者:支豪亮
 */
public class AppExecutors {
    /**
     * 文件操作的线程池
     */
    private final Executor mDiskIO;

    /**
     * 网络连接操作的线程池
     */
    private  final Executor mNetWork;

    /**
     * UI线程池的操作
     */
    private  final Executor mMainThread;


    public AppExecutors(){
        //单线程的线程池
        mDiskIO = Executors.newSingleThreadExecutor();
        //最多可以存在三个线程的线池
        mNetWork = Executors.newFixedThreadPool(3);

        mMainThread = new MainThreadExcutor();

    }

    public Executor getDiskIO() {
        return mDiskIO;
    }

    public Executor getNetWork() {
        return mNetWork;
    }

    public Executor getMainThread() {
        return mMainThread;
    }

    private static class MainThreadExcutor implements Executor{

        private Handler mainThradHander = new Handler(Looper.getMainLooper()) ;

        @Override
        public void execute(@NonNull  Runnable command) {
            mainThradHander.post(command);

        }
    }
}
